#!/usr/bin/env python
#coding=utf8

from app.connector.submail_connector import SubmailConnector
from config.connector_config import ConnectorConfDev as config

class Factory(object):

    _INSTANCE_DICT = {}

    def __init__(self):
        raise NotImplementedError

    @classmethod
    def get_instance(cls, name):
        raise NotImplementedError

class ConnectorFactory(Factory):
    """
    连接器的简单工厂
    """

    @classmethod
    def get_instance(cls, name):
        name = name.lower()
        if not cls._INSTANCE_DICT.has_key("_connector"):
            cls._INSTANCE_DICT['_connector'] = {}
        if not cls._INSTANCE_DICT['_connector'].get(name):
            _connector = getattr(ConnectorFactory, "_create_%s_connector" % name, None)()
            if _connector:
                cls._INSTANCE_DICT['_connector'][name] = _connector
        return cls._INSTANCE_DICT['_connector'].get(name)

    @staticmethod
    def _create_submail_connector():
        return SubmailConnector(config.SUBMAIL_CLIENT_ID, config.SUBMAIL_SECRET_KEY,config.SUBMAIL_DOMAIN, config.SUBMAIL_CLIENT_ID)
